package cn.yj.frame.spring.event;

import cn.yj.frame.netty.NettyServer;
import cn.yj.frame.spring.RpcServiceBeanPostProcessor;
import cn.yj.register.RegisterConfig;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * 用于发布RPC对应的服务进行做接口暴露
 *
 * @author 永健
 * @since 2022-01-19 10:23
 */
public class RpcSpringApplicationStartingListener implements ApplicationListener<ApplicationStartedEvent> {


	public static Map<String, Object> SERVICE_BEAN_CACHE = new ConcurrentHashMap<>();


	@Override
	public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
		if (Objects.nonNull(SERVICE_BEAN_CACHE)) {
			Map<String, RpcServiceBeanPostProcessor> rpcServiceBeanPostProcessorMap = applicationStartedEvent.getApplicationContext()
				.getBeansOfType(RpcServiceBeanPostProcessor.class);
			SERVICE_BEAN_CACHE.forEach((beanName, bean) -> {
				rpcServiceBeanPostProcessorMap.forEach((b, processor) -> {
					processor.postProcessAfterInitialization(bean, beanName);
				});
			});
			SERVICE_BEAN_CACHE = null;
		}
	}

}
